<template>

  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="100px"
    >
      <el-form-item label="实验室名称" prop="stuId">
        <el-input
          v-model.number="queryParams.stuId"
          class="!w-240px"
          clearable
          placeholder="请输入实验室名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search"/>
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh"/>
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column align="center" label="实验室编号" prop="code"/>
      <el-table-column align="center" label="实验室类型" prop="type">
        <template #default="scope">{{labStatusFilter(scope.row.type)}}</template>
      </el-table-column>
      <el-table-column align="center" label="实验室名称" prop="name"/>
      <el-table-column align="center" label="容纳人数" prop="capacity"/>
      <el-table-column align="center" label="说明" prop="explanation"/>
      <el-table-column align="center" label="负责人" prop="owner"/>
      <el-table-column :width="300" align="center" label="操作">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update',scope)">预约</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <RoleForm ref="formRef" @success="getList"/>
  <!-- 表单弹窗：文件上传 -->
  <FileForm ref="formUpdateRef" @success="getList"/>
</template>
<script lang="ts" setup>
import download from '@/utils/download'
import * as RoleApi from '@/api/system/role'
import * as ProjectApi from '@/api/laboratory/lab'
import RoleForm from './RoleForm.vue'
import FileForm from './UploadForm.vue'

defineOptions({name: 'SystemRole'})

const message = useMessage() // 消息弹窗
const {t} = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
})
const queryFormRef = ref() // 搜索的表单


/** 查询角色列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProjectApi.getLaboratoryPage(queryParams)
    console.log(data)
    list.value = data.result
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, row:object) => {
  formRef.value.open(type,row)
}

// 获取实验室类型
const getLabType = async () =>{
  const aa = await ProjectApi.getLaboratoryTypeList({pageNo:1,pageSize:100,dictType:'laboratory'})
  let arr = []
  aa.list.forEach(item => {
    arr.push({
      label: item.label,
      value: item.value
    })
  })
  return arr;
}

const labStatusList = ref([])
const labStatusFilter = (value) => {
  let v = ""
  console.log('入参为',value)
  labStatusList.value.forEach(i => {
    if(i.value == value){
      v = i.label
    }
  })
  return v;
}

/** 初始化 **/
onMounted(async () => {
  getList()
  labStatusList.value = await getLabType()
  console.log(' labStatusList.value', labStatusList.value)
})
</script>
